package net.tngou.tpage.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import net.tngou.tpage.cache.CacheEngine;
import net.tngou.tpage.cache.EhCacheEngine;
import net.tngou.tpage.jdbc.Inflector;
import net.tngou.tpage.jdbc.OrderType;
import net.tngou.tpage.jdbc.PageUtil;
import net.tngou.tpage.pojo.POJO;








public class BaseService {

	public PageUtil getPage(int page,int size,Class<? extends POJO> pojoClass) {
		PageUtil pageUtil = null;
			try {
				POJO pojo = pojoClass.newInstance();
				pageUtil = new PageUtil(pojo.list(page, size),page,size,pojo.totalCount());
		
			} catch (InstantiationException | IllegalAccessException e) {
			
				e.printStackTrace();
			}
		
		return pageUtil;
		
	}
	
	public PageUtil getPage(int page,int size,String type,OrderType order,Class<? extends POJO> pojoClass) {
		
		PageUtil pageUtil = null;
		try {
			POJO pojo = pojoClass.newInstance();
			pageUtil=new PageUtil(pojo.list(page, size, type, order),page,size,pojo.totalCount());
	
		} catch (InstantiationException | IllegalAccessException e) {
		
			e.printStackTrace();
		}	
		return pageUtil;
		
	}
	
	public PageUtil getPage(int page,int size,Map<String, Object> likes,Class<? extends POJO> pojoClass) {
		
		if(likes==null||likes.isEmpty())
		{
			return getPage(page, size, pojoClass);
		}
		PageUtil pageUtil = null;
			try {
				POJO pojo = pojoClass.newInstance();
				pageUtil = new PageUtil(pojo.list(page, size,likes),page,size,pojo.totalCount(likes));
		
			} catch (InstantiationException | IllegalAccessException e) {
			
				e.printStackTrace();
			}
		
		return pageUtil;
		
	}
	
	
	public PageUtil getPage(int page,int size,String filter,Map<String, Object> likes,Class<? extends POJO> pojoClass) {
		
		if(likes==null||likes.isEmpty())
		{
			return getPage(page, size,filter, pojoClass);
		}
		PageUtil pageUtil = null ;
			try {
				POJO pojo = pojoClass.newInstance();
				pageUtil = new PageUtil(pojo.list(filter,likes,page, size),page,size,pojo.totalCount(filter,likes));
				
			} catch (InstantiationException | IllegalAccessException e) {
			
				e.printStackTrace();
			}
		return pageUtil;		
	}
	
	public PageUtil getPage(int page,int size,String filter,Class<? extends POJO> pojoClass) {
		
		PageUtil pageUtil = null ;
			try {
				POJO pojo = pojoClass.newInstance();
				pageUtil = new PageUtil(pojo.list(filter,page, size),page,size,pojo.totalCount(filter));
				
			} catch (InstantiationException | IllegalAccessException e) {
			
				e.printStackTrace();
			}
		return pageUtil;		
	}
		
	public List<?> getList(Class<? extends POJO> pojoClass) {
	
		List<?> list = null;
		
			try {
				POJO pojo = pojoClass.newInstance();
				list=pojo.list();
				
			} catch (InstantiationException | IllegalAccessException e) {
			
				e.printStackTrace();
			}
		
		return list;
		
	}
	
	
	public List<?> getList(String type,OrderType orderType,Class<? extends POJO> pojoClass) {

		List<?> list = null;
		
			try {
				POJO pojo = pojoClass.newInstance();
				list=pojo.list(type, orderType);
				
			} catch (InstantiationException | IllegalAccessException e) {
			
				e.printStackTrace();
			}
		
		return list;
		
	}
	public List<?> getList(String filter,String type,OrderType orderType,Class<? extends POJO> pojoClass) {

		List<?> list = null;
		
		try {
			POJO pojo = pojoClass.newInstance();
			list=pojo.list(filter, type, orderType);
			
		} catch (InstantiationException | IllegalAccessException e) {
		
			e.printStackTrace();
		}

	return list;
		
	}
	public List<?> getList(String filter,Class<? extends POJO> pojoClass) {
	
		List<?> list = null;
		
			try {
				POJO pojo = pojoClass.newInstance();
				list=pojo.list(filter, "id", OrderType.DESC);
				
			} catch (InstantiationException | IllegalAccessException e) {
			
				e.printStackTrace();
			}

		return list;
		
	}
	
	
  protected static CacheEngine cacheEngine =new  EhCacheEngine().getInstance();
	
	
	
      
	
}
